package dispatcher.group.gui.floor.state;

import java.util.Hashtable;

import state.Globals;
import state.group.events.BaseEvent;
import state.group.events.EventCollection;
import state.group.events.HallCallEvent;
import system.Constant;

/**
 * This class store the collection of floor status that are in the elevator system
 * @author Caleb
 *
 */
public class FloorDataCollection extends Hashtable<Byte, FloorStruct> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5227084809399329089L;
	
	/**
	 * The default constructor
	 */
	public FloorDataCollection(){
		
	}
	
	/**
	 * This method will create structs for each of the floors from floor 1 to maxFloor inclusively.
	 * @param maxFloor The number of floors to generate
	 */
	public void setMaxFloor(int maxFloor){
		// Checks if number of floors is zero
		if (maxFloor < 0){
			return;
		}
		// Clears the list
		this.clear();
		
		// Creates the floor structs from 1 - maxFloor inclusively
		synchronized(this){
			
			for(int i = 0; i < maxFloor; i++){
				byte id = (byte)(i + Constant.DEFAULT_FLOOR_START);
				this.put(id, new FloorStruct(id));
			}
		}
	}

	/**
	 * This method will update the appropriate floors structs depending on the events given
	 * @param collection The collection of events to update structs
	 */
	public void updateStructUsingEvents(EventCollection collection){
		// TODO: Check for concurrency issues
		for(BaseEvent e : collection){
			switch(e.getEventType()){
			
				// It will update the structs if a hall call event has be passed in
				case HallCallEvent:
					Globals.getLogger().log("Event Update is : " + e.getEventType(), this.getClass().getName(), "updateStructUsingEvents");
					HallCallEvent event = (HallCallEvent)e;
					// Updates struct
					FloorStruct relatedStruct = get(event.getHallCall().getFloor());
					if (relatedStruct != null){
						relatedStruct.updateStruct(event);
					}
					break;
			}
		}
	}

}
